// pagesMine/pages/housingResource/housingResource.js
import ajax from '../../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 详情ID
     */
    detailId: null,
    /**
     * 房产ID
     */
    houseId: null,
    /**
     * 页面步骤 1 基本信息  2 上传图片
     */
    nextNumber: 1,
    /**
     * 装修情况数组
     */
    fitmentCaseArr: ['精装修', '简装', '毛坯'],
    formData: {
      id: null,
      type: 0, // 0 房东 1 经纪人
      fitmentCase: 0, // 装修情况
      housingRentImage: [], // 房产图片id
      indexImage: null, // 封面图
      housingAssessId: null, // 房产id
      housingMasterId: null, // 房东id
      paymentMethod: null, // 付款方式
      univalenceDay: null, // 单价/天
      univalenceMonth: null, // 单价/月
      workCount: null // 工位数量
    },
    /**
     * 单价/月 获取焦点状态
     */
    univalenceMonthFocus: false,
  },
  /**
   * 单选按钮事件
   */
  fitmentCaseEvent(event) {
    const { formData } = this.data
    const value = event.currentTarget.dataset.index
    if (formData.fitmentCase == value) return
    formData.fitmentCase = value
    this.setData({ formData })
  },
  /**
   * input 变化事件
   */
  formItemChange(event) {
    const { formData } = this.data
    const key = event.currentTarget.dataset.key
    const value = event.detail.value || event.detail
    formData[key] = value
    this.setData({ formData })
  },
  /**
   * 单价input 获取焦点
   */
  univalenceDayConfirm(event) {
    if (!event.detail) return
    this.setData({ univalenceMonthFocus: true })
  },
  /**
   * 上传图片
   */
  async uploadImage(event) {
    const key = event.currentTarget.dataset.key
    const { formData } = this.data
    const imagePath = await ajax.chooseImage()
    const res = await ajax.uploadFile({ filePath: imagePath })
    const image = res.data.url, housingRentId = formData.id
    if (housingRentId && key != 'indexImage') ajax.post('/linkwechat/image/add', { housingRentId, image })
    key == 'indexImage' ? formData.indexImage = image : formData.housingRentImage.push({ image })
    this.setData({ formData })
  },
  /**
   * 删除图片事件
   */
  deleteImageEvent(event) {
    const { index, item } = event.currentTarget.dataset;
    const { formData } = this.data
    wx.showModal({
      title: '提示',
      content: '确定删除该图片吗?',
      success: async (res) => {
        if (!res.confirm) return;
        if (item.id) await ajax.get(`/my/house/rent/remove?id=${item.id}`)
        formData.housingRentImage.splice(index, 1);
        ajax.showSuccess('操作成功!')
        this.setData({ formData })
      }
    })
  },
  /**
   * 房产认证详情
   */
  async getHouseDetailRequest() {
    const { houseId } = this.data
    const res = await ajax.get(`/my/house/assess/info?id=${houseId}`)
    const entity = res.data
    this.data.formData.housingAssessId = entity.id
    this.data.formData.housingMasterId = entity.housingMasterId
    this.setData({ detailEntity: entity })
  },
  /**
   * 房源详情
   */
  async getDetailRequest() {
    const { formData } = this.data
    const res = await ajax.get(`/my/house/rent/info?id=${formData.id}`)
    this.setData({ formData: res.data })
  },
  /**
   * 提交事件
   */
  async submitEvent() {
    const { nextNumber, formData, houseId } = this.data
    if (!formData.workCount) return ajax.showToast('请填写工位数')
    if (!formData.paymentMethod) return ajax.showToast('请填写付款方式')
    if (!formData.univalenceDay) return ajax.showToast('请填写单价天价格')
    if (!formData.univalenceMonth) return ajax.showToast('请填写单价月价格')
    if (nextNumber == 1) return this.setData({ nextNumber: 2 })
    // 房源信息预览
    if (nextNumber == 2) {
      if (!formData.indexImage) return ajax.showToast('请上传封面图')
      if (!formData.housingRentImage) return ajax.showToast('请上传房源图片')
      ajax.showLoading()
      const res = await ajax.get(`/my/house/rent/housingPreview?housingAssessId=${houseId}`).finally(() => wx.hideLoading())
      console.log(res.data)
      res.data.houseRent = formData
      res.data.housingRentImage = formData.housingRentImage
      const data = escape(JSON.stringify(res.data))
      wx.navigateTo({
        url: `/pagesEnterprise/pages/houseDetail/houseDetail?detail=${data}`,
        events: { 
          backEvent: () => {
            setTimeout(async() => {
              await ajax.showSuccess('请等待审核');
              wx.navigateBack()
            }, 1000)
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.houseId = options.houseId || null
    this.data.formData.id = options.detailId || null
    this.data.formData.type = options.type || 0
    this.getHouseDetailRequest()
    if (options.detailId) this.getDetailRequest()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})